<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>千禧宝贝连锁店管理信息系统</title>
<%@ include file="../../common/Style.jsp"%>
<%@ page import="com.onlineMIS.common.Common_util"%>
<script>
$(document).ready(function(){	
	parent.$.messager.progress('close'); 
});
var baseurl = "<%=request.getContextPath()%>";
var index = <s:property value="formBean.expenseHq.list.size()"/>;
var freeType = <s:property value="formBean.expenseHq.type"/>;
var headqSelectKey = <%=Common_util.ALL_RECORD%>;
/**
 * 创建总部费用-单据过账
 */
function post(){
	
	var categoryId = $("#financeCategory_ID").combobox('getValue');
	
	if(categoryId != headqSelectKey){
		if ( $('#expenseForm').form('validate')){
			//创建总部费用单据
			if(this.freeType == 0){
				$.messager.confirm('费用单据', '是否确定创建费用单据?', function(r){
		            if (r){
		            	var params=$("#expenseForm").serialize();
		            	$.post("financeHQJSON!postExpense",params, updateBKProcess,"json");	
		            }
		        });
			}else{
				//创建总部收益单据
				$.messager.confirm('收益单据', '是否确定创建收益费用单据?', function(r){
		            if (r){
		            	var params=$("#expenseForm").serialize();
		            	$.post("financeHQJSON!postExpense",params, updateBKProcess,"json");	
		            }
		        });
			}

		}
	}else{
		$.messager.alert('提示','支出账目为空,请选择支出账目后再提交!','warning');
	}
	
	

}

/**
 * 创建总部费用-保存草稿
 */
function saveDraft(){
	
	var categoryId = $("#financeCategory_ID").combobox('getValue');
	
	if(categoryId != headqSelectKey){
		if ( $('#expenseForm').form('validate')){
	    	var params=$("#expenseForm").serialize();
    		$.post("financeHQJSON!saveDraftExpense",params, updateBKProcess,"json");	
		}
	}else{
		$.messager.alert('提示','支出账目为空,请选择支出账目后再提交!','warning');
	}
	
}

/**
 * 单据过账，保存草稿 - 回调函数
 */
function updateBKProcess(data){
	var returnCode = data.returnCode;
	if (data.success){
		$.messager.alert({
			icon: 'info',
			title: '保存成功',
			msg: data.message,
			fn: function(){
				window.location.href = "financeHQJSP!editExpense?formBean.expenseHq.type="+ freeType;
			}
		});		
	}    
	else {
		 $.messager.alert('提示', data.message, 'warning');
	}		
}



/**
 * 新增一行费用明细
 */
function addNewRow(){
	
		$("#row0").remove(); 
		var num = index +1;
		var expenseType = $("#expenseHqType_ID").combobox('getValue');
		if(expenseType == headqSelectKey){
			$.messager.alert('提示', '请选择费用类型', 'warning');
			return ;
		}
		
		if (!is_number(expenseType)){
			$.messager.alert('提示', '当前费用类别id出现问题,请联系系统管理员', 'error');
			return ;
		}
		//文本字符
		var expenseTypeText = $("#expenseHqType_ID").combobox('getText');

		var str = "";
		str += "<tr height='32' id='row"+ num + "'  class='InnerTableContent'>";
		str += "<td align='center'>" +(num)+ "</td>";
		str += "<td align='center'><input type='hidden' id='expenseType"+num+"' name='formBean.expenseHq.list["+index+"].expenseHqType.id'  value="+expenseType+"><input type='text' readonly='readonly'  value="+expenseTypeText+"  class='easyui-textbox' ></td>";
		str += "<td align='center'><input type='text' name='formBean.expenseHq.list["+index+"].amount' id='amount"+index+"' data-options='onChange:function(param1, param2){sumAmount();},min:0,max:200000,precision:2' onfocus='this.select();' value='0' class='easyui-numberbox' ></td>";
		str += "<td align='center'><input type='text' name='formBean.expenseHq.list["+index+"].comment' id='comment"+index+"'  class='easyui-textbox' ></td>";
		str += "<td align='center'><img src='"+baseurl+"/conf_files/web-image/delete.png' border='0' onclick='deleteRow("+num+")'  style='cursor:pointer;'/></td>";
		str += "</tr>";
		$("#expenseDetail").append(str);
		$.parser.parse($("#org_table"));	//动态添加的时候样式失效的问题,不指定div，就是全局刷新
        index++;
}

/**
 * 前台计算合计金额（注：后台也有金额合计计算）
 */
function sumAmount(){
        var sumAmount  = 0;
		for(var rows=0;rows<index;rows++){
			var rowOb = $("#amount"+rows).val();			
			if(rowOb != null){
				var rowAmount = $("#amount"+rows).numberbox('getValue');
				sumAmount += parseFloat(rowAmount);
			}
			
		}		
		$("#sumAmount").textbox('setValue',sumAmount);

}


/**
 * 删除一行信息
 */
function deleteRow(index){
	$("#row"+index).remove(); 
    sumAmount();
}



function confirmIsDelete(data){
	if(data != null){
		var returnCode = data.returnCode;
		$.messager.alert('提示',data.message,'info');				
	}						
}

</script>
</head>
<body>
   <s:form action="" method="POST"  name="expenseForm" id="expenseForm" theme="simple"> 
	<table width="95%" align="center"  class="OuterTable" >
	    <tr><td>
	     <s:hidden name="formBean.expenseHq.id"></s:hidden>
	     <s:hidden name="formBean.expenseHq.type"></s:hidden>
		 <table width="100%" border="0">
	        	<tr class="PBAOuterTableTitale">

		       		<td height="50" width="10%" colspan="6"><s:property value="formBean.expenseHq.typeHQS"/> &nbsp&nbsp单据编号:&nbsp<s:property value="formBean.expenseHq.id"/> &nbsp &nbsp &nbsp &nbsp &nbsp 单据状态：
		       			<s:property value="formBean.expenseHq.statusS"/></td>
				</tr>

			<tr height="10"></tr>
	        <tr class="InnerTableContent">
				<td align="center"><strong>单据日期 :</strong></td>
				<td align="center"><s:textfield name="formBean.expenseHq.expenseDate" 
						cssClass="easyui-datebox"/></td>
						
				<td align="center"><strong>支出账目 :</strong></td>
				<td ><s:select name="formBean.expenseHq.financeCategory.id" id="financeCategory_ID"
						cssClass="easyui-combobox" style="width:250px;" data-options="prompt:'请选择支出账目'"
						list="uiBean.financeCategory" listKey="id" listValue="itemName"  headerKey="-1" headerValue=""/></td>
			
					<td align="center"><strong>费用类型 :</strong></td>
					<td ><s:select  cssClass="easyui-combobox" style="width:230px;" id="expenseHqType_ID" data-options="prompt:'请选择费用类型'"
						list="uiBean.expenseHqTypes" listKey="id" listValue="name" headerKey="-1" headerValue=""/>					
						<a id="btn2" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="addNewRow();">添加</a> 											
					</td>	
			</tr>

				<tr class="InnerTableContent">
							<td height="4" colspan="6"><hr width="100%" color="#FFCC00" /></td>
						</tr>
				<tr class="InnerTableContent">
						<td colspan="6"><table  align="left" class="OuterTable" border="0" width="100%" height="100px"
									id="org_table">
									<tr class="PBAInnerTableTitale" align='left'>
										<th width="10%" height="35" align="center">序号</th>
										<th width="15%" align="center">费用类型</th>
										<th width="15%" align="center">金额</th>
										<th width="35%" align="center">备注信息</th>
										<th width="5%" align="center">删除</th>
									</tr>
									<tbody  id="expenseDetail">
									<s:iterator value="formBean.expenseHq.list" status = "st" id="items" >
										<tr id="row<s:property value="#st.index+1"/>"  class="InnerTableContent">
											<td align="center"><s:property value="#st.index +1"/>

											</td>
											<td align="center"><input type="text" class="easyui-textbox" readonly="readonly"  name="formBean.expenseHq.list[<s:property value="#st.index"/>].expenseHqType.name"  value="<s:property value="#items.expenseHqType.parentName" />:<s:property value="#items.expenseHqType.name" />"/>
											<input type="hidden" name="formBean.expenseHq.list[<s:property value="#st.index"/>].expenseHqType.id"  value="<s:property value="#items.expenseHqType.id" />"/></td>
											<td align="center"><input type="text" class="easyui-numberbox" name="formBean.expenseHq.list[<s:property value="#st.index"/>].amount"  data-options='onChange:function(param1, param2){sumAmount();},min:0,max:200000,precision:2' onfocus='this.select();' id="amount<s:property value="#st.index"/>"  value="<s:property value="#items.amount" />"/></td>
											<td align="center"><input type="text" class="easyui-textbox" name="formBean.expenseHq.list[<s:property value="#st.index"/>].comment"  value="<s:property value="#items.comment" />"/></td>
											<td align="center"><img src="<%=request.getContextPath()%>/conf_files/web-image/delete.png" border='0' onclick='deleteRow("<s:property value="#st.index+1" />")'  style='cursor:pointer;'/></td>
										</tr>
									</s:iterator>
									
									<s:if test="formBean.expenseHq.id == 0">
										<tr height="30" class="InnerTableContent" id="row0">
											<td align="center" colspan="5">暂无费用类型明细！</td>										
										</tr>	
									</s:if>
									</tbody>
									<tr class="PBAInnerTableTitale">						
										<td width="10%" align="center"><strong>合计金额:</strong></td>
										<td></td>	
										<td align="center"><input  type='text' readonly="readonly"  id="sumAmount" data-options="min:0,max:200000,precision:2"   name="formBean.expenseHq.amount" value="<s:property value="formBean.expenseHq.amount" />"  class="easyui-numberbox"  /></td>					
									    <td></td>
									    <td></td>
									</tr>																								
														
									</table>
								</td>									
					</tr>

				<tr class="InnerTableContent">						
					 <td width="10%" align="center"><strong>单据备注:</strong></td>
					<td  align="left" colspan="5"><s:textarea  type='text' readonly="readonly"  id="comment" rows="1" cols="80"  name="formBean.expenseHq.comment"  cssClass="easyui-textbox"  data-options="prompt:'请输入单据备注信息'"/></td>
						
				</tr>
				<tr style="display:none">
					 <td width="10%" colspan="5"></td>				
				</tr>
					<tr class="InnerTableContent">
						<td></td>
						<td colspan="5">
							 <a id="btn2" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="saveDraft();">保存草稿</a> 
							 <a id="btn2" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="post();">单据过账</a> 

						</td>
					</tr>
	 </table>
	 </s:form>
   
</body>
</html>